function [DDmax,desviacion_tipica_DD,media_DD,media_dias_DD,desviacion_tipica_dias_DD,DDactual,inicioDD,finDD] = drawdownstudy( output_balance )
%% DRAWDOWNSTUDY Summary of this function goes here
%NumeroPicos=5;%Numero de maximos que queremos encontrar
data=output_balance(:,1);
PicosMaximo=zeros(1,2);

fechaPrimera =734139;
PicosMaximo(1,1)=data(1,1);
PicosMaximo(1,2)=1;
maximo = data(1,1);
posicion=1;
indice=1;
%[PicosMaximo(indice,1),PicosMaximo(indice,2)]=max(data);
for i = 1:length(output_balance(:,1))
        if data(i,1)>maximo
            maximo=data(i,1);
            posicion=i;
            %PicosMaximo(indice,1)=data(i,1);
            %PicosMaximo(indice,2)=i;
           

        end
        if data(i,1)<maximo
                
                indice=indice+1;
                PicosMaximo(indice,1)=maximo;
                PicosMaximo(indice,2)=posicion;
                maximo=0;
                
              
        end
    
end
if length(PicosMaximo(:,1))>1
        PicosMaximo(1,:)=[];
else 
        indice=indice+1;
                PicosMaximo(indice,1)=maximo;
                PicosMaximo(indice,2)=posicion;
                maximo=0;
end
%%
PicosMaximo2=PicosMaximo;
contadorresta=0;
for filas=2:length(PicosMaximo(:,1))
    
   if  PicosMaximo(filas,1)<PicosMaximo(filas-1,1)
       PicosMaximo2(filas-contadorresta,:)=[];
       contadorresta=contadorresta+1;
   end
end
PicosMaximo=PicosMaximo2;

% while recorre <= length(output_balance(:,1))
%     [valor,recorre]=max(data(PicosMaximo(indice,2):end,1));
%     if valor > PicosMaximo(indice,1)
%         
%         indice=indice+1;
%         PicosMaximo(indice,1)=valor;
%         PicosMaximo(indice,2)= posicion;
%         data(PicosMaximo(indice,2))=0;
%     %[PicosMaximo(indice,1),PicosMaximo(indice,2)]=max(data(PicosMaximo(indice-1,2):end,1));
%     end
%     if recorre<PicosMaximo(indice,2)
%         break
%     end    
% end
%% Picos minimo
NumeroPicos=length(PicosMaximo(:,1));
PicosMaximo=sortrows(PicosMaximo,2);
indice=1;
data=output_balance(:,1);
for maximos=1:NumeroPicos
    if indice<NumeroPicos
        if PicosMaximo(indice+1,2)-1 > PicosMaximo(indice,2)+1%solo para ahora
            [PicosMinimo(indice,1),posicion]=min(data(PicosMaximo(indice,2)+1:PicosMaximo(indice+1,2)-1));
        else
            if indice>0
            [PicosMinimo(indice,1)]=data(PicosMaximo(indice+1,2)+1);%-------------------------------------------VERSION 2.0
            end
        end
    end
    if indice ==NumeroPicos
    [PicosMinimo(indice,1),posicion]=min(data(PicosMaximo(indice,2)+1:end));    
    end
    PicosMinimo(indice,2)=posicion+PicosMaximo(indice,2);
    %data(PicosMinimo(indice,2))=NaN;
    indice=indice+1;
end
%%   
[DDmax,posicionmaximo]=max(PicosMaximo(:,1)-PicosMinimo(:,1));
DDvertical = PicosMaximo(:,1)-PicosMinimo(:,1);
DDhorizontal = PicosMinimo(:,2)-PicosMaximo(:,2);
desviacion_tipica_dias_DD = std(DDhorizontal);
desviacion_tipica_DD=std(DDvertical);
media_DD=mean(DDvertical);
media_dias_DD=mean(DDhorizontal);

DDactual=0;
if PicosMinimo(end,2)==today
    DDactual=PicosMaximo(end,1)-PicosMinimo(end,1);
end
inicioDD = PicosMaximo(posicionmaximo,2)+fechaPrimera;
finDD = PicosMinimo(posicionmaximo,2)+fechaPrimera;
end

